package adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.thinkland.juheapi.demo.R;

import java.util.List;

/**
 * Created by Administrator on 15-11-30.
 */
public class CityListAdatper extends BaseAdapter {
    private List<String> list;
    private LayoutInflater mInflater;
    //记录城市名称索引
    private List<String> listSoryKeys;
    //用于检测排序字母是否存在stringbuffer
    private StringBuffer stringBuffer = new StringBuffer();
    //用于根据List项检测是否存在SortKey
    private List<String> firstLetter;


    public CityListAdatper(Context context, List<String> list, List<String> listSoryKeys) {
        this.list = list;
        mInflater = LayoutInflater.from(context);
        this.listSoryKeys = listSoryKeys;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public String getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {

        View rowView = null;
        if (convertView == null) {
            rowView = mInflater.inflate(R.layout.item_city_list, null);
        } else {
            rowView = convertView;
        }
        String cityName = getItem(position);
        String sort = listSoryKeys.get(position).toUpperCase().charAt(0)+"";
        TextView tv_sortKey = (TextView) rowView.findViewById(R.id.tv_city_list_item_sort);

        if(stringBuffer.indexOf(sort) == -1){
            stringBuffer.append(sort);
            listSoryKeys.add(cityName);
        }
        if(listSoryKeys.contains(cityName)){
            tv_sortKey.setText(sort);
            tv_sortKey.setVisibility(View.VISIBLE);
        }else{
            tv_sortKey.setVisibility(View.GONE);
        }
        TextView tv_city = (TextView) rowView.findViewById(R.id.tv_city);
        tv_city.setText(getItem(position));
//        Log.i("TAG", listSoryKeys.get(position).toUpperCase().charAt(0));

        return rowView;
    }
}
